home *** CD-ROM | disk | FTP | other *** search
/ CD World Haziran 1997 / CD World Haziran 1997.iso / Cesitlemeler / Directx 3.0 / dx3.exe / SDK / SAMPLES / DUEL / INPUT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-30  |  4.5 KB  |  199 lines

  1. /*==========================================================================
  2.  *
  3.  *  Copyright (C) 1995-1996 Microsoft Corporation. All Rights Reserved.
  4.  *
  5.  *  File:       input.c
  6.  *  Content:    DirectInput functionality for Multi-player duel
  7.  *
  8.  *
  9.  ***************************************************************************/
  10.  
  11.  
  12. #include "input.h"
  13. #include "gameproc.h"
  14.  
  15. extern HINSTANCE                ghinst;             // program instance
  16. extern HWND                     ghWndMain;              // app window handle
  17.  
  18. static LPDIRECTINPUT            lpdi;               // DirectInput interface
  19. static LPDIRECTINPUTDEVICE      lpdiKeyboard;           // keyboard device interface
  20. static BOOL                     fKeybdAcquired;     // has the keyboard been acquired?
  21.  
  22. extern DWORD                    gdwKeys;            // gameplay keys
  23.  
  24. /*
  25. *
  26. * InitInput
  27. *
  28. * Initialize DirectInput objects & devices
  29. *
  30. */
  31. BOOL InitInput(void)
  32. {
  33.     GUID        guid = GUID_SysKeyboard;
  34.    HRESULT  hRes;
  35.  
  36.     // try to create di object
  37.     if(DirectInputCreate(ghinst, DIRECTINPUT_VERSION, &lpdi, NULL) != DI_OK)
  38.     {
  39.         ShowError(IDS_DINPUT_ERROR_DIC);
  40.         return FALSE;
  41.     }
  42.  
  43.  
  44.     // try to create keyboard device
  45.         if(lpdi->lpVtbl->CreateDevice(lpdi, &guid, &lpdiKeyboard, NULL) !=DI_OK)
  46.     {
  47.         ShowError(IDS_DINPUT_ERROR_CD);
  48.         return FALSE;
  49.     }
  50.  
  51.         // Tell DirectInput that we want to receive data in keyboard format
  52.         if (lpdiKeyboard->lpVtbl->SetDataFormat(lpdiKeyboard, &c_dfDIKeyboard) != DI_OK)
  53.         {
  54.                 ShowError(IDS_DINPUT_ERROR_DF);
  55.                 return FALSE;
  56.         }
  57.  
  58.         // set cooperative level
  59.         if(lpdiKeyboard->lpVtbl->SetCooperativeLevel(lpdiKeyboard, ghWndMain,
  60.                          DISCL_NONEXCLUSIVE | DISCL_FOREGROUND) != DI_OK)
  61.     {
  62.         ShowError(IDS_DINPUT_ERROR_SP);
  63.         return FALSE;
  64.         }
  65.  
  66.     // try to acquire the keyboard
  67.    hRes = lpdiKeyboard->lpVtbl->Acquire(lpdiKeyboard);
  68.    if(SUCCEEDED(hRes))
  69.    {
  70.       // keyboard was acquired
  71.       fKeybdAcquired = TRUE;
  72.     }
  73.    else
  74.    {
  75.       // keyboard was NOT acquired
  76.       fKeybdAcquired = FALSE;
  77.    }
  78.  
  79.     // if we get here, all objects were created successfully
  80.     return TRUE;    
  81.         
  82. }
  83.  
  84.  
  85. /*
  86. *
  87. * DI_ReadKeys
  88. *
  89. * Use DirectInput to read game-play keys
  90. *
  91. */
  92. void DI_ReadKeys(void)
  93. {
  94.     BYTE rgbKeybd[256];
  95.    HRESULT hRes;
  96.  
  97.    hRes = lpdiKeyboard->lpVtbl->GetDeviceState(lpdiKeyboard, sizeof(rgbKeybd), rgbKeybd);
  98.    if(hRes != DI_OK)
  99.     {
  100.       if(hRes == DIERR_INPUTLOST)
  101.       {
  102.          // we lost control of the keyboard, reacquire
  103.          fKeybdAcquired = FALSE;
  104.          if(SUCCEEDED(lpdiKeyboard->lpVtbl->Acquire(lpdiKeyboard)))
  105.          {
  106.             fKeybdAcquired = TRUE;
  107.          }
  108.       }
  109.  
  110.       // failed to read the keyboard, just return
  111.         return;
  112.     }
  113.  
  114.     // reset key states
  115.     gdwKeys = gdwKeys ^ gdwKeys;
  116.  
  117.     // check & update key states
  118.     if(rgbKeybd[DIK_NUMPAD5] & 0x80)
  119.         gdwKeys |= KEY_STOP;
  120.  
  121.     if((rgbKeybd[DIK_NUMPAD2] & 0x80) || (rgbKeybd[DIK_DOWN] & 0x80))
  122.         gdwKeys |= KEY_DOWN;
  123.  
  124.     if((rgbKeybd[DIK_NUMPAD4] & 0x80) || (rgbKeybd[DIK_LEFT] & 0x80))
  125.         gdwKeys |= KEY_LEFT;
  126.  
  127.     if((rgbKeybd[DIK_NUMPAD6] & 0x80) || (rgbKeybd[DIK_RIGHT] & 0x80))
  128.         gdwKeys |= KEY_RIGHT;
  129.  
  130.     if((rgbKeybd[DIK_NUMPAD8] & 0x80) || (rgbKeybd[DIK_UP] & 0x80))
  131.         gdwKeys |= KEY_UP;
  132.  
  133.     if(rgbKeybd[DIK_SPACE] & 0x80)
  134.         gdwKeys |= KEY_FIRE;
  135.  
  136. }
  137.  
  138. /*
  139. *
  140. * CleanupInput
  141. *
  142. * Cleans up DirectInput objects
  143. *
  144. */
  145. void CleanupInput(void)
  146. {
  147.     if(fKeybdAcquired)
  148.     {
  149.         lpdiKeyboard->lpVtbl->Unacquire(lpdiKeyboard);
  150.         fKeybdAcquired = FALSE;
  151.     }
  152.  
  153.     if(lpdiKeyboard != NULL)
  154.         lpdiKeyboard->lpVtbl->Release(lpdiKeyboard);
  155.  
  156.     if(lpdi!= NULL)
  157.         lpdi->lpVtbl->Release(lpdi);
  158.     
  159. }
  160.  
  161.  
  162. /*
  163. *
  164. * ReacquireInputDevices
  165. *
  166. * Reacquires DirectInput devices as needed
  167. *
  168. */
  169. BOOL ReacquireInputDevices(void)
  170. {
  171.     // try to acquire the keyboard
  172.         if(lpdiKeyboard != NULL)
  173.         {
  174.                 lpdiKeyboard->lpVtbl->Acquire(lpdiKeyboard);
  175.          }
  176.         else
  177.         {
  178.                 // keyboard device has not been created.
  179.                 fKeybdAcquired = FALSE;
  180.                 return FALSE;
  181.         }
  182.  
  183.         // if we get here, we are acquired again
  184.         fKeybdAcquired = TRUE;
  185.         return TRUE;
  186. }
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.